/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * exitFrame.java
 *
 * Created on Oct 31, 2011, 7:39:06 PM
 */

package giormi;

import javax.swing.JFrame;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;

/**
 *
 * @author Ling
 */
public class confirmFrame extends javax.swing.JFrame {

    /** Creates new form exitFrame */
    private MainFrame main;
    private JFrame frame;
    private Object ob;
    private String record;
    private F_addorderstocks oss;
    protected JTable table;
    private boolean update;
    private F_updatecustomer uuc;
    private F_updateproduct uup;
    private F_updateorder uuo;
    private F_updatestock uus;

    public confirmFrame(MainFrame mf, JFrame prev, Object obj, String rec, boolean update) {
        initComponents();
        this.setLocation(440, 240);
        this.setSize(380, 170);
        this.setResizable(false);
        this.setDefaultCloseOperation(javax.swing.JFrame.DO_NOTHING_ON_CLOSE);
        this.main=mf;
        this.frame=prev;
        this.ob=obj;
        this.record=rec;
        this.update = update;
    }


    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        addsuccess1 = new javax.swing.JLabel();
        addsuccess = new javax.swing.JLabel();
        cancelButton = new javax.swing.JButton();
        jLabel1 = new javax.swing.JLabel();
        okButton = new javax.swing.JButton();
        addsuccess2 = new javax.swing.JLabel();

        addsuccess1.setText("Confirm Change. Are you sure you wish to");

        setDefaultCloseOperation(javax.swing.WindowConstants.DO_NOTHING_ON_CLOSE);
        setTitle("Confirm Changes");
        getContentPane().setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());

        addsuccess.setText("make the following changes?");
        getContentPane().add(addsuccess, new org.netbeans.lib.awtextra.AbsoluteConstraints(130, 20, -1, 80));

        cancelButton.setIcon(new javax.swing.ImageIcon(getClass().getResource("/giormi/sources/Close-2-icon.png"))); // NOI18N
        cancelButton.setText("Cancel");
        cancelButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cancelButtonActionPerformed(evt);
            }
        });
        getContentPane().add(cancelButton, new org.netbeans.lib.awtextra.AbsoluteConstraints(250, 90, 90, -1));

        jLabel1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/giormi/sources/Button-Warning-icon (1).png"))); // NOI18N
        getContentPane().add(jLabel1, new org.netbeans.lib.awtextra.AbsoluteConstraints(20, 20, -1, -1));

        okButton.setIcon(new javax.swing.ImageIcon(getClass().getResource("/giormi/sources/Ok-icon.png"))); // NOI18N
        okButton.setText("OK");
        okButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                okButtonActionPerformed(evt);
            }
        });
        getContentPane().add(okButton, new org.netbeans.lib.awtextra.AbsoluteConstraints(140, 90, 90, -1));

        addsuccess2.setText("Confirm Change. Are you sure you wish to");
        getContentPane().add(addsuccess2, new org.netbeans.lib.awtextra.AbsoluteConstraints(120, 10, -1, 70));

        pack();
    }// </editor-fold>//GEN-END:initComponents

    public void setTable(JTable table) {
        this.table = table;
    }
    private void cancelButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cancelButtonActionPerformed
        // TODO add your handling code here:
        frame.setEnabled(true);
        this.setVisible(false);
        this.dispose();
}//GEN-LAST:event_cancelButtonActionPerformed

    public void addRow (JTable table, String record){
        System.out.println("table:"+table);
        if (record.equalsIgnoreCase("stock") || record.equalsIgnoreCase("order"))
        table.setAutoResizeMode(javax.swing.JTable.AUTO_RESIZE_SUBSEQUENT_COLUMNS);

        DefaultTableModel model = (DefaultTableModel) table.getModel();
        model.insertRow(model.getRowCount(), new Object[]{""});

        if (record.equalsIgnoreCase("stock") || record.equalsIgnoreCase("order"))
            table.setAutoResizeMode(javax.swing.JTable.AUTO_RESIZE_OFF);
}
    private void okButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_okButtonActionPerformed
        // TODO add your handling code here:
        if (record.equals("product")){
            F_addproduct p = new F_addproduct();
            F_updateproduct up = new F_updateproduct();
            viewProducts v_p = new viewProducts(table);
            if (update){
                up = (F_updateproduct) ob;
                up.connect();
                up.updateproduct();
                up.disconnect();
            }
            else {
                p = (F_addproduct) ob;
                p.connect();
                p.addproduct();
                p.disconnect();
            }
            
            //clear
            addRow(this.table,"product");
            v_p.connect();
            v_p.viewProducts();
            v_p.disconnect();
        }
            else if (record.equals("stock")){
                F_addstock s = new F_addstock();
                F_updatestock us = new F_updatestock();
                viewStocks v_s = new viewStocks(table);
                if (update){
                    us = (F_updatestock) ob;
                    us.connect();
                    us.updatestock();
                    us.disconnect();
                }
                 else{
                     s = (F_addstock) ob;
                     s.connect();
                     s.addstock();
                     s.disconnect();
                 }
                
                //clear
                addRow(this.table,"stock");
                v_s.connect();
                v_s.viewStocks();
                v_s.disconnect();
                }
                else if(record.equals("order")){
                    F_addorder o = new F_addorder();
                    F_updateorder uo = new F_updateorder();
                    viewOrders v_o = new viewOrders(table);

                    if(update){
                        uo.setPrev(frame);
                        uo.connect();
                        uo = (F_updateorder) ob;
                        uo.updateorder();
                        uo.disconnect();
                    }
                    else{
                        o = (F_addorder) ob;
                        o.connect();
                        o.addorder();
                        o.updatestock();
                        o.disconnect();
                    }
                    //clear
                    addRow(this.table,"order");
                    v_o.connect();
                    v_o.viewOrders();
                    v_o.disconnect();
                }
                    else if(record.equals("customer")){
                        F_addcustomer c = new F_addcustomer();
                        F_updatecustomer uc = new F_updatecustomer();
                        viewCustomers v_c = new viewCustomers(table);

                        if(update){
                            uc = (F_updatecustomer) ob;
                            uc.connect();
                            uc.updatecustomer();
                            uc.disconnect();
                        }
                        else{
                            c = (F_addcustomer) ob;
                            c.connect();
                            c.addcustomer();
                            c.disconnect();
                        }
                        //clear
                        addRow(this.table,"customer");
                        v_c.connect();
                        v_c.viewCustomers();
                        v_c.disconnect();
                    }

        this.setVisible(false);
        this.dispose();
        successFrame su = new successFrame(frame, main);
        su.setVisible(true);
    }//GEN-LAST:event_okButtonActionPerformed


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLabel addsuccess;
    private javax.swing.JLabel addsuccess1;
    private javax.swing.JLabel addsuccess2;
    private javax.swing.JButton cancelButton;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JButton okButton;
    // End of variables declaration//GEN-END:variables

}
